package com.gq.utils;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class MySQLiteHelper extends SQLiteOpenHelper{

	private static final String TAG = MySQLiteHelper.class.getSimpleName();
	public static final String DATABASE_NAME = "portal.db";
	public static final int DATABASE_VERSION = 1;
	public static final String TABLE_STORY = "story";
	
	
	public MySQLiteHelper(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		Log.d(TAG, "onCreate");
		db.execSQL(String.format("CREATE TABLE %s(id VARCHAR(32) UNIQUE PRIMARY KEY NOT NULL, title VARCHAR(100) NOT NULL, author VARCHAR(50), numChapter INTEGER DEFAULT 0, favorite INTEGER DEFAULT 0, lastvisit INTEGER DEFAULT 0, lastchapter INTEGER DEFAULT 0);", TABLE_STORY));
		db.execSQL(String.format("CREATE INDEX story_idx ON %S(id, favorite)", TABLE_STORY));
		db.execSQL(String.format("CREATE INDEX story_idx_history ON %S(lastvisit)", TABLE_STORY));
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		Log.d(TAG, "call Upgrade database");
		db.execSQL("DROP TABLE IF EXISTS " + TABLE_STORY);
		onCreate(db);
	}
	

}
